package im.yisme.view.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import im.yisme.R;
import im.yisme.model.service.ModelServiceProvider;
import im.yisme.model.service.MusicRepositoryService;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import im.yisme.view.ui.MainView;
import im.yisme.view.ui.animation.Rotate3D;
import im.yisme.view.ui.util.MusicListAdapter;
import im.yisme.vo.MusicVO;
import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class PlayMusicListActivity extends Rotate3DActivity{
	private ViewService viewCtrl;
	private MusicRepositoryService model;
	private ViewGroup view;
	private MainView mainView;
  
	private ListView list;
    private ArrayList<MusicVO> musicVOList;
    
    private ImageButton backButton;
		
	protected void onCreate(Bundle savedInstanceBundle) {
		super.onCreate(savedInstanceBundle);
		setContentView(R.layout.activity_musiclist);
		
		
		viewCtrl=ViewServiceProvider.getViewService();
		model = ModelServiceProvider.getMusicRepositoryService();
		
		view = (RelativeLayout) findViewById(R.id.musiclist);
		initButton();
		
		list = (ListView) findViewById(R.id.listView_music);
		
		Iterator<MusicVO> it = model.getMusicList();
		 ArrayList<Map<String, Object>> data1=new ArrayList<Map<String,Object>>();
			
	     musicVOList = new ArrayList<MusicVO>();	
	     
	        while (it.hasNext()) {
				MusicVO musicVO = it.next();
				Map<String,Object> item = new HashMap<String,Object>();  
		            item.put("lyricsName", musicVO.getName());   
		            item.put("duration", musicVO.getDuration());  
		            data1.add(item);   
		        musicVOList.add(musicVO);
			}

	        
//	        Map<String,Object> item = new HashMap<String,Object>();
//	        item.put("lyricsName", "阿姆斯壮");  
//	        
//	        Map<String,Object> item2 = new HashMap<String,Object>();
//	        item2.put("lyricsName", "again");  
//
//	        Map<String,Object> item3 = new HashMap<String,Object>();
//	        item3.put("lyricsName", "again");  
//	        
//	        data2.add(item);data2.add(item2);data2.add(item3);
//	        
	       
	      MusicListAdapter adapter = new MusicListAdapter(this, data1);
	      
	      list.setAdapter(adapter);
	      
	      list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if(arg2 == 0 || arg2 == musicVOList.size()+1) return;
				jumpToPlay(musicVOList.get(arg2-1));
				overridePendingTransition(android.R.anim.fade_in, R.anim.musiclist_activity_zoomin);
			}
		});
		
	}
	
	private void jumpToPlay(MusicVO musicVO){
		Intent intent = new Intent(this,PlayLyricsListActivity.class);
		intent.putExtra("musicVO", musicVO);
		viewCtrl.jumpTo(intent);
	}
	
	private void jumpToMain(){
		Intent intent = new Intent(this,MainActivity.class);
		viewCtrl.jumpTo(intent);
	}
	
	protected void onResume(){
		super.onResume();
		viewCtrl.updateTopActivity(this);
	}
	protected void onStop(){
		super.onStop();
		if(mainView != null) mainView.stop();
	}
	private void back(){
		jumpToMain();
		overridePendingTransition(0, 0);
	}
	
	public void onBackPressed(){}
	
	private void initButton(){
		backButton = (ImageButton) findViewById(R.id.imageButton_back);
		backButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				view.startAnimation(lOutAnimation);
				mainView = new MainView(PlayMusicListActivity.this);
				setContentView(mainView);
				mainView.startAnimation(rInAnimation);
				rInAnimation.setAnimationListener(new AnimationListener() {
					
					public void onAnimationStart(Animation animation) {}
					
					public void onAnimationRepeat(Animation animation) {}
					
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						back();
					}
				});
				
			}
		});
	}
}
